<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/3 0003
 * Time: 17:53
 */

/**
 * RabbitMQ单例对象
 */

namespace libs;

use PhpAmqpLib\Connection\AMQPStreamConnection;

class RabbitMQ
{
    private static $connection;
    private static $_instance;

    private function __construct()
    {
        //rabbitMQ配置
        $config = config('rabbitMQ');

        self::$connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['pwd']);
        self::$_instance = self::$connection->channel();
    }

    //获取静态实例
    public static function getRabbitMQ()
    {
        if (!self::$_instance) {
            new self;
        }
        return self::$_instance;
    }

    public static function close()
    {
        self::$_instance->close();
        self::$connection->close();
    }

    /*
     * 禁止clone
     */
    private function __clone()
    {
    }
}